package com.wenhui.nzapp.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.gzwh.weblib.bean.DinnerNzVerify;
import com.gzwh.weblib.bean.Result;
import com.gzwh.weblib.utils.MyObserver;
import com.gzwh.weblib.utils.RequestUtils;
import com.wenhui.nzapp.AppContext;
import com.wenhui.nzapp.R;
import com.wenhuitec.syslib.config.SysLoginConfig;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class LoginActivity extends AppCompatActivity {

    private Context mContext;

    private EditText userNameEdt;

    private EditText passwordEdt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        mContext = this;
        userNameEdt = findViewById(R.id.userNameEdt);
        passwordEdt = findViewById(R.id.passwordEdt);
        SysLoginConfig.getInstance(AppContext.getInstance());
        if (SysLoginConfig.getInstance(AppContext.getInstance()).isStatus()) {
            Date date = SysLoginConfig.getInstance(AppContext.getInstance()).getLoginTime();
            Calendar calendar = Calendar.getInstance();
            calendar.setTime(date);
            calendar.add(Calendar.DATE, 1);
            date = calendar.getTime();
            System.out.println("需要校验时间时间：" + new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date));
            if (date.compareTo(new Date()) > 0) {
                System.out.println("有效码在内，存在有效，跳过校验登录");
                Intent intent = new Intent(mContext, MainActivity.class);
                startActivity(intent);
                finish();
            }
        }


        findViewById(R.id.loginBut).setOnClickListener(view -> {

            String userName = userNameEdt.getText().toString();
            String password = passwordEdt.getText().toString();
            if (userName == null || "".equals(userName)) {
                Toast.makeText(mContext, "用户名不能为空", Toast.LENGTH_SHORT).show();
                return;
            }
            if (password == null || "".equals(password)) {
                Toast.makeText(mContext, "密码不能为空", Toast.LENGTH_SHORT).show();
                return;
            }

            RequestUtils.getLogin(mContext, userName, password, "www", new MyObserver<String>(mContext, true) {
                @Override
                public void onSuccess(Result<String> result) {
                    SysLoginConfig.getInstance(AppContext.getInstance()).setUserName(userName);
                    SysLoginConfig.getInstance(AppContext.getInstance()).setPassword(password);
                    SysLoginConfig.getInstance(AppContext.getInstance()).setToken(result.getResult());
                    SysLoginConfig.getInstance(AppContext.getInstance()).setStatus(true);
                    SysLoginConfig.getInstance(AppContext.getInstance()).setLoginTime(new Date());
                    SysLoginConfig.getInstance(AppContext.getInstance()).setLastTime(new Date());
                    Intent intent = new Intent(mContext, MainActivity.class);
                    startActivity(intent);
                    finish();
                }

                @Override
                public void onFailure(Throwable e, String errorMsg) {
                    Toast.makeText(mContext, errorMsg, Toast.LENGTH_SHORT).show();
                }
            });
        });
    }
}
